/**
 * 初始化 echarts(line和bar) 的封装
 *
 * 约定：
 *
 * @author fhw
 */
(function () {
    var BasicEchart = function (echartId, url,type,name) {
    	debugger;
        this.echartId = echartId;
        this.url = url;
        this.queryParams = {}; // 向后台传递的自定义参数
        this.option={};
        this.name=name;
        this.type=type;// line,bar
    };

    BasicEchart.prototype = {
        /**
         * 初始化echart
         */
        init: function () {
            this.option= {
            	    title: {
            	        text: this.name
            	    },
            	    tooltip: {
            	    },
            	    toolbox: {
            	        show: true,
            	        orient: 'vertical',
            	        left: 'right',
            	        top: 'center',
            	        feature: {
            	            mark: {show: true},
            	            dataView: {show: true, readOnly: false},
            	            magicType: {show: true, type: ['line', 'bar']},
            	            restore: {show: true},
            	            saveAsImage: {show: true}
            	        }
            	    },
            	    legend: {
            	        data:['']
            	    },
            	    xAxis:  {
            	    },
            	    yAxis: {
            	        
            	    },
            	    series: [
            	        {
            	            name:'',
            	            type:this.type,
            	            itemStyle: {
            	                normal: {
            	                    color: new echarts.graphic.LinearGradient(
            	                        0, 0, 0, 1,
            	                        [
            	                            {offset: 0, color: '#83bff6'},
            	                            {offset: 0.5, color: '#188df0'},
            	                            {offset: 1, color: '#188df0'}
            	                        ]
            	                    )
            	                },
            	                emphasis: {
            	                    color: new echarts.graphic.LinearGradient(
            	                        0, 0, 0, 1,
            	                        [
            	                            {offset: 0, color: '#2378f7'},
            	                            {offset: 0.7, color: '#2378f7'},
            	                            {offset: 1, color: '#83bff6'}
            	                        ]
            	                    )
            	                }
            	            },
            	        }
            	    ]
            	};
            var curechartId=this.echartId;
        	var curoption=this.option;
            $.ajax({
		    	type: "POST",
				url: this.url,
				async:false,
				data:this.queryParams,
				dataType: "json",
				success: function(data){
					debugger;
					curoption.xAxis.data=[];
					curoption.series[0].data=[];
					for(var i=0;i<data.length;i++){
						curoption.xAxis.data.push(data[i].data);
						curoption.series[0].data.push(data[i].num);
	    	        };
	    	        var myChart = echarts.init(document.getElementById(curechartId));
	    	  	    myChart.clear();
	    	  	    myChart.setOption(curoption, true); 
				}
				
			});
            return this;
        }
    };
    window.BasicEchart = BasicEchart;
}());
